package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.MyFanEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SP_viewpager_adapter extends PagerAdapter {
    private Context context;
    private String[] titles;
    private ArrayList<View> views;  //每个选项卡对应的布局
    private ArrayList<SP_recyleview_adapter> adapters=new ArrayList<>();


    public SP_viewpager_adapter(String[] titles, ArrayList<View> views, Context context,ArrayList<SP_recyleview_adapter> adapters){
        this.titles=titles;
        this.views=views;
        this.context=context;
        this.adapters=adapters;
    }

    //实例化选项卡
    @NotNull
    @SuppressLint("ShowToast")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = views.get(position);
        container.addView(v);
        //所有页面布局一样
        RecyclerView recyclerView=v.findViewById(R.id.recyceleview_searchvideo);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapters.get(position));

        setSpanner(v,position,adapters.get(position));
        return v;
    }

    //删除选项卡
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
    //返回选项大小
    @Override
    public int getCount() {
        return views.size();
    }

    //判断是否是为返回选项卡
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    //获取标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    void setSpanner(View v,int position,SP_recyleview_adapter adapter){
        Spinner spinner=(Spinner) v.findViewById(R.id.Sort_select);
        List<String> list = new ArrayList<String>();
        if (position!=5){
            list.add("时间");
            list.add("播放");
        }
        else {
            list.add("粉丝");
            list.add("视频");
        }
        ArrayAdapter<String> spinner_adapter=new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,list);
        spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinner_adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                System.out.println(position+"ss"+i);
                if (position<5){
                    List<VideoEntity> videoEntities=new ArrayList<>();
                    videoEntities=adapter.getVideosData(position);
                    //按时间排序
                    if (i==0){
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {
                                return o2.getVideo_pub_time().compareTo(o1.getVideo_pub_time());
                            }
                        });
                    }else {//按播放量
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {
                                if (o2.getTotalplay()>o1.getTotalplay())
                                    return 1 ;
                                else
                                    return -1;
                            }
                        });
                    }

                    adapter.setVideosData(videoEntities,position);
                }
                else {
                    List<MyFanEntity> users=new ArrayList<>();
                    users=adapter.getUserData();
                    //按粉丝数
                    if (i==0){
                        Collections.sort(users, new Comparator<MyFanEntity>() {
                            public int compare(MyFanEntity o1, MyFanEntity o2) {
                                int fs1=Integer.parseInt(o1.getFan_count());
                                int fs2=Integer.parseInt(o2.getFan_count());
                                if (fs1<fs2)
                                    return 1;
                                else
                                    return -1;
                            }
                        });
                    }else {//按视频数
                        Collections.sort(users, new Comparator<MyFanEntity>() {
                            public int compare(MyFanEntity o1, MyFanEntity o2) {
                                int fs1=Integer.parseInt(o1.getVideo_count());
                                int fs2=Integer.parseInt(o2.getVideo_count());
                                if (fs1<fs2)
                                    return 1;
                                else
                                    return -1;
                            }
                        });
                    }

                    adapter.setUserData(users);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}
